/*
 * 功能：用户评论信息控制器，处理与评论相关的HTTP请求
 * 作者：张杨(Sunny)
 * 日期： 2024/11/21 下午2:19
 */
package com.example.springboot.control;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Comment;
import com.example.springboot.service.CommentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 用户评论信息控制器，提供车辆相关的CRUD（创建读取更新删除）操作
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    CommentService commentService;

    //创建评论，自动填充当前用户信息。
    @PostMapping("/create")
    public Result create(@RequestBody Comment comment) {
        return commentService.createComment(comment);
    }

    // 更新评论
    @PutMapping("/update")
    public Result update(@RequestBody Comment comment) {
        commentService.updateById(comment);
        return Result.success();
    }

    // 删除评论
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        commentService.removeById(id);
        return Result.success();
    }

    // 批量删除评论
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        commentService.removeBatchByIds(ids);
        return Result.success();
    }

    // 查询自己发布的评论
    @GetMapping("/selectSelf")
    public Result selectSelf() {
        List<Comment> commentList = commentService.selectSelfComments();
        return Result.success(commentList);
    }

    // 查询公开评论
    @GetMapping("/selectOpen")
    public Result selectOpen() {
        List<Comment> commentList = commentService.selectOpenComments();
        return Result.success(commentList);
    }

    // 分页查询评论信息，并支持根据多个条件进行筛选
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String title, // 标题
                               @RequestParam String username, // 用户名
                               @RequestParam String createTime, // 创建时间
                               @RequestParam String content, // 内容
                               @RequestParam String coach, // 教练
                               @RequestParam String score) { // 评分
        Page<Comment> page = commentService.selectByPage(pageNum, pageSize, title, username, createTime, content, coach, score);
        return Result.success(page);
    }

    //导出信息到本地 Excel 文件
    @AuthAccess
    @GetMapping("/export")
    public void exportData(@RequestParam(required = false) String username,
                           @RequestParam(required = false) String createTime,
                           @RequestParam(required = false) String ids,
                           HttpServletResponse response) throws IOException {
        commentService.exportData(username, createTime, ids, response);
    }
}
